package com.bestv.search.engine.command;

import com.bestv.search.common.cache.SystemCache;
import com.bestv.search.common.dataSource.DataSourceKey;
import com.bestv.search.common.dataSource.DataSourceKeyUtil;
import com.bestv.search.common.model.Info;
import com.bestv.search.common.model.InfoHeader;
import com.bestv.search.common.service.InfoHeaderManager;
import com.bestv.search.common.util.CharUtil;
import com.bestv.search.common.util.ChineseCharToEn;
import com.bestv.search.common.util.CommonUtil;
import com.bestv.search.engine.compass.model.HdInfoLucene;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class InfoHeaderCommand {

    private static SimpleDateFormat updatetimeformat = new SimpleDateFormat("yyyyMMddHHmmss");
    private static SimpleDateFormat yearformat = new SimpleDateFormat("yyyy");
    private InfoHeaderManager infoHeaderManager;
    private SystemCache systemCache;

    public List<InfoHeader> getAllValidHeaders() {
        DataSourceKey key = DataSourceKeyUtil.convert(systemCache.getLocalDataSourceKey());
        return infoHeaderManager.getAllValidHeader(key);
    }

    public List<InfoHeader> getHeadersByUpdateTime(Date updateTime) {
        DataSourceKey key = DataSourceKeyUtil.convert(systemCache.getLocalDataSourceKey());
        return infoHeaderManager.getHeadersByUpdateTime(key, updateTime);
    }

    public List<Info> convertToInfoLucene(List<InfoHeader> headers) {
        DataSourceKey key = DataSourceKeyUtil.convert(systemCache.getLocalDataSourceKey());
        List<Info> results = new ArrayList<Info>();
        if (headers != null) {
            for (InfoHeader header : headers) {
                Info info = new Info();
                info.setId(header.getId());
                info.setName(header.getName());

                // Gets namePY
                String abbreviation = ChineseCharToEn.getAllFirstLetter(header.getName());
                String splitTokens = CommonUtil.splitTokenByPaoding(header.getName());
                info.setNamePY(abbreviation + " " + CharUtil.charToNum(abbreviation) + " " + splitTokens);

                String genre = header.getGenre();
                if (genre != null) {
                    if (genre.endsWith(";")) {
                        genre = genre.substring(0, genre.length() - 1);
                    }
                }
                info.setGenre(genre);
                info.setYear(String.valueOf(header.getReleased() == null ? 0 : yearformat.format(header.getReleased())));

                // Gets default play code
                info.setCode(infoHeaderManager.getDefaultCodeByHeaderId(key, header.getId()));
                info.setUpdateTimeMillis(Long.valueOf(updatetimeformat.format(header.getLast_update_time())));

                // 0：没有高清和标清 1：只有标清 2： 只有高清 3：有高清和标清
                if (header.getClarity_flag() == 2) {
                    info.setHd(1);
                } else {
                    info.setHd(0);
                }

                info.setIsCharge(header.getIsCharge());
                info.setInfoFlag(2);
                info.setHeaderType(header.getHeaderType());
                info.setContent_flag(1);
                info.setP_type(header.getP_type());
                info.setBizType(header.getBizType());
                info.setUp_flag(header.getUp_flag());
                info.setInfoCount(infoHeaderManager.getItemCountByHeaderId(key, header.getId()));
                if (header.getStatus() == 0 || info.getInfoCount() == 0) {
                    info.setUpdateFlag(2);
                } else {
                    info.setUpdateFlag(1);
                }

                results.add(info);
            }
        }
        return results;
    }

    public List<HdInfoLucene> convertToHdInfoLucene(List<InfoHeader> headers) {
        List<HdInfoLucene> results = new ArrayList<HdInfoLucene>();
        DataSourceKey key = DataSourceKeyUtil.convert(systemCache.getLocalDataSourceKey());
        if (headers != null) {
            for (InfoHeader header : headers) {
                HdInfoLucene info = new HdInfoLucene();
                info.setId(String.valueOf(header.getId()));
                info.setName(header.getName());
                if (header.getName() != null && header.getName().length() >= 1) {
                    info.setNameFirstChar(header.getName().substring(0, 1));
                }

                info.setNameForSort(header.getName());

                // Set namePy
                String luceneNamePY = ChineseCharToEn.getAllFirstLetter(header.getName());
                info.setNamePY(luceneNamePY);
                if (luceneNamePY != null && luceneNamePY.length() >= 1) {
                    info.setNamePYFirstChar(luceneNamePY.substring(0, 1));
                }

                // Gets default play code
                info.setCode(infoHeaderManager.getDefaultCodeByHeaderId(key, header.getId()));
                info.setBizTypeValue(header.getBizType().getValue());
                info.setBizTypeLevel(header.getBizType().getLevel());
                info.setP_type(header.getP_type());

                String genre = header.getGenre();
                if (genre != null) {
                    if (genre.endsWith(";")) {
                        genre = genre.substring(0, genre.length() - 1);
                    }
                }
                info.setGenre(genre);
                info.setRegion(header.getRegion());
                info.setTags(header.getTags());
                info.setCategory(header.getCategory());
                info.setLanguage(header.getLanguage());
                info.setHitCount(infoHeaderManager.getTotalHitCountByHeaderId(key, header.getId()));
                info.setIsCharge(header.getIsCharge());

                // 0：没有高清和标清, 1：只有标清 , 2： 只有高清, 3：有高清和标清
                if (header.getClarity_flag() == 2) {
                    info.setHd(1);
                } else {
                    info.setHd(0);
                }

                info.setYear(header.getReleased() == null ? 0
                        : Integer.valueOf(yearformat.format(header.getReleased())));
                info.setUpdateTimeMillis(Long.valueOf(updatetimeformat.format(header.getLast_update_time())));
                info.setInfoFlag(2);
                info.setContent_flag(1);
                info.setUp_flag(header.getUp_flag());
                info.setHeaderType(header.getHeaderType());
                info.setInfoCount(infoHeaderManager.getItemCountByHeaderId(key, header.getId()));
                if (header.getStatus() == 0 || info.getInfoCount() == 0) {
                    info.setUpdateFlag(2);
                } else {
                    info.setUpdateFlag(1);
                }

                results.add(info);
            }
        }

        return results;
    }

    public InfoHeaderManager getInfoHeaderManager() {
        return infoHeaderManager;
    }

    public void setInfoHeaderManager(InfoHeaderManager infoHeaderManager) {
        this.infoHeaderManager = infoHeaderManager;
    }

    public SystemCache getSystemCache() {
        return systemCache;
    }

    public void setSystemCache(SystemCache systemCache) {
        this.systemCache = systemCache;
    }

}